// JavaScript Document
var registerAction = {
    email: '',
    password: '',
    nickname: '',
    taobaoID: '',
    gender: '',
    blurEmailInput: function() {
        var emailaddress = $("#emailaddress").val();
        var offset = $('#emailaddress').position();
        var posTop = offset['top'] - 10;
        var posLeft = offset['left'] + 200;
        if (emailaddress.length == 0) {
            showStatusIconOnRegForm('emailaddress', 'warning');
            showMsgOnRegForm('notice', '请填写有效的电子邮箱，该邮箱将成为你在兔丝街的登录帐号，同时用于找回密码。', posTop, posLeft);
            return false;
        }
        if (!isEmail(emailaddress)) {
            showStatusIconOnRegForm('emailaddress', 'wrong');
            showMsgOnRegForm('warning', '电子邮箱格式有误，请重输！', posTop, posLeft);
            return false;
        }
        var checkUserExist = checkIfEmailExist(emailaddress);
        if (checkUserExist != "0") {
            showStatusIconOnRegForm('emailaddress', 'wrong');
            showMsgOnRegForm('warning', '这个邮箱已经注册过兔丝街了，请输入其他邮箱。', posTop, posLeft);
            return false;
        }
        showStatusIconOnRegForm('emailaddress', 'good');
        hideMsgWarningOnRegForm();
        hideMsgOnRegFrom();
        registerAction.email = emailaddress;
        return true;
    },
    focusEmailInput: function() {
        registerAction.blurEmailInput();
        return false;
    },	
    blurNicknameInput: function() {
        var nickname = $('#nickname').val().replace(/(^\s*)|(\s*$)/g, "");
        var isAT = checkNicknameSymbol(nickname);
        var offset = $('#nickname').position();
        var posTop = offset['top'] - 10;
        var posLeft = offset['left'] + 200;
        if (isAT == true) {
            showHintInfo("#nickname_hint", "<img src='css/images/confirm.gif' style='float:left'/>" + "<p style='float:left; line-height:200%'>&nbsp;昵称不能包含空格和以下符号：# @ :</p>");
            return false;
        }
        if (nickname.length == 0) {
            showStatusIconOnRegForm('nickname', 'warning');
            showMsgOnRegForm('notice', '你喜爱的称呼，中英文下划线都可以。', posTop, posLeft);
            return false;
        }
        var nickname_length = checkStringLength(nickname);
        if (nickname_length > 20) {
            showStatusIconOnRegForm('nickname', 'wrong');
            showMsgOnRegForm('notice', '昵称长度不能超过20个字母或10个汉字。', posTop, posLeft);
            return false;
        }
        var checkUserExist = checkIfNickExist(nickname);
        if (checkUserExist == "5") {
            showStatusIconOnRegForm('nickname', 'wrong');
            showMsgOnRegForm('warning', '哎呀，这个昵称太受欢迎了，已经有兔丝在使用了，起个新的个性昵称吧！', posTop, posLeft);
            return false;
        }
        if (checkUserExist == "4") {
            showStatusIconOnRegForm('nickname', 'wrong');
            showMsgOnRegForm('warning', '哎呀，这个昵称太受欢迎了，已经有兔丝在使用了，起个新的个性昵称吧！', posTop, posLeft);
            return false;
        }
        if (checkUserExist == "3") {
            showStatusIconOnRegForm('nickname', 'wrong');
            showMsgOnRegForm('warning', '支持中英文、数字、下划线，限长10个汉字。', posTop, posLeft);
            return false;
        }
        if (checkUserExist == "2") {
            showStatusIconOnRegForm('nickname', 'wrong');
            showMsgOnRegForm('warning', '哎呀，这个昵称太受欢迎了，已经有兔丝在使用了，起个新的个性昵称吧！', posTop, posLeft);
            return false;
        }
        showStatusIconOnRegForm('nickname', 'good');
        hideMsgWarningOnRegForm();
        hideMsgOnRegFrom();
        registerAction.nickname = nickname;
        return true;
    },
    focusNicknameInput: function() {
        registerAction.blurNicknameInput();
        return false;
    },
    blurPasswordInput: function() {
        var offset = $('#password').position();
        var posTop = offset['top'] - 10;
        var posLeft = offset['left'] + 200;
        var password = $("#password").val().replace(/(^\s*)|(\s*$)/g, "");
        if (password.length == 0) {
            showStatusIconOnRegForm('password', 'warning');
            showMsgOnRegForm('notice', '6位到32位，英文字母、数字或符号。', posTop, posLeft);
            return false;
        }
        if (password.length < 6 || password.length > 32) {
            showStatusIconOnRegForm('password', 'wrong');
            showMsgOnRegForm('warning', '输入密码需在6位到32位之间。', posTop, posLeft);
            return false;
        }
        showStatusIconOnRegForm('password', 'good');
        hideMsgWarningOnRegForm();
        hideMsgOnRegFrom();
        registerAction.password = password;
        return true;
    },
    focusPasswordInput: function() {
        registerAction.blurPasswordInput();
        return false;
    },
    blurVPasswordInput: function() {
        var password = $("#password").val().replace(/(^\s*)|(\s*$)/g, "");
        var offset = $('#confirm_password').position();
        var posTop = offset['top'] - 10;
        var posLeft = offset['left'] + 200;
        var confirm_password = $("#confirm_password").val().replace(/(^\s*)|(\s*$)/g, "");
        if (confirm_password.length == 0) {
            showStatusIconOnRegForm('confirm_password', 'warning');
            showMsgOnRegForm('notice', '再输入一次密码', posTop, posLeft);
            return false;
        }
        if (confirm_password.length < 6 || confirm_password.length > 32) {
            showStatusIconOnRegForm('confirm_password', 'wrong');
            showMsgOnRegForm('warning', '输入密码需在6位到32位之间。', posTop, posLeft);
            return false;
        }
        if (confirm_password != password) {
            showStatusIconOnRegForm('confirm_password', 'wrong');
            showMsgOnRegForm('warning', '两次密码输入不一致，请重新输入', posTop, posLeft);
            return false;
        }
        showStatusIconOnRegForm('confirm_password', 'good');
        hideMsgWarningOnRegForm();
        hideMsgOnRegFrom();
        return true;
    },
    focusVPasswordInput: function() {
        registerAction.blurVPasswordInput();
        return false;
    },
    blurGenderRadio: function() {
        var offset = $('#genderM').position();
        var posTop = offset['top'] - 17;
        var posLeft = offset['left'] + 146;
        var genderM = $('#genderM').val();
        var genderF = $('#genderF').val();
        var gender = '';
        if ($('#genderM').attr('checked') == true) {
            gender = genderM;
        }
        if ($('#genderF').attr('checked')) {
            gender = genderF
        }
        registerAction.gender = gender;
        if (gender == undefined || gender == '') {
            showStatusIconOnRegForm('genderM', 'warning');
            showMsgOnRegForm('warning', '请选择您的性别！~', posTop, posLeft);
            return false;
        }
        showStatusIconOnRegForm('genderM', 'good');
        hideMsgWarningOnRegForm();
        hideMsgOnRegFrom();
        return true;
    },
    focusGenderRadio: function() {
        var offset = $('#genderM').position();
        var posTop = offset['top'] - 17;
        var posLeft = offset['left'] + 146;
        showMsgOnRegForm('notice', '请选择您的性别！~', posTop, posLeft);
        registerAction.blurGenderRadio();
        showStatusIconOnRegForm('genderM', 'good');
        hideMsgWarningOnRegForm();
        hideMsgOnRegFrom();
        return false;
    },
    checkRegisterForm: function() {
        var boolCheck = false;
        boolCheck = registerAction.blurEmailInput();
        if (boolCheck == false) {
            return false;
        }
        boolCheck = registerAction.blurPasswordInput();
        if (boolCheck == false) {
            return false;
        }
        boolCheck = registerAction.blurVPasswordInput();
        if (boolCheck == false) {
            return false;
        }
        boolCheck = registerAction.blurNicknameInput();
        if (boolCheck == false) {
            return false;
        }
        boolCheck = registerAction.blurGenderRadio();
        if (boolCheck == false) {
            return false;
        }
		return true;
    }
};
//绝对定位
function showMsgOnRegForm(msgType, msgContent, posTop, posLeft) {
    hideMsgOnRegFrom();
    var msgID;
    if (msgType == 'notice') {
        msgID = 'regNotice';
    } else {
        msgID = 'regWarning';
    }
    $('#' + msgID + ' .messageBox').html(msgContent);
    $('#' + msgID).css('left', posLeft);
    $('#' + msgID).css('top', posTop);
    $('#' + msgID).show();
    $('.bottom-left-t').css('width', '158px');
    $('.bottom-left-w').css('width', '158px');
}
function hideMsgOnRegFrom() {
    $('#regNotice').hide();
}
function hideMsgWarningOnRegForm() {
    $('#regWarning').hide();
}
//不同同状态的图标
function showStatusIconOnRegForm(currentObj, iconType) {
    var obj = $('#' + currentObj).parents('.inputBox').parents('.oneline').find('.hints');
    obj.css("width", '25px');
    obj.css("height", '25px');
    if (iconType == 'wrong') {
        obj.css("background", "url('../images/register/wrong.gif')");
    } else if (iconType == 'warning') {
        obj.css("background", "url('../images/register/warning.gif')");
    } else if (iconType == 'good') {
        obj.css("background", "url('../images/register/good.gif')");
    }
    return true;
}
function checkNicknameSymbol(nickname) {
    return new RegExp("[ |:|：|@|#]{1,}", 'gi').test(nickname);
}
//电子邮件是否正确
function trim(str)
{
	return str;	
}
function isEmail(Str) {
    var Pat = /^([a-zA-Z0-9_\-\.])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
    if (!Pat.exec(Str)) {
        return false;
    }
    var Table = new Array("\"", "\'", "<", ">", "~", "\\", ";", ",", "?", "/");
    var I, J;
    for (I = 0; I < Str.length; I++) {
        for (J = 0; J < Table.length; J++) {
            if (Str.charAt(I) == Table[J]) {
                return false;
            }
        }
    }
    return true;
}
//判断用户名重复
function checkIfEmailExist(emailaddress) {
    var url = 'http://www.tusijie.com/user/check_user.php?email='+emailaddress;
    res = $.ajax({
        async: false,
        type: "GET",
        url: url,
    }).responseText;
	var resObj = eval('(' + res + ')');
	if (resObj.code==1)
		return resObj.has_user;

    return 0;
}
//判断昵称
function checkStringLength(words) {
    var sum = 0;
    for (var i = 0; i < words.length; ++i) {
        var c = words.charCodeAt(i);
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
            sum++;
        } else {
            sum += 2;
        }
    }
    return sum;
}
//判断电子邮件重复
function checkIfEmailExist(emailaddress) {
    var url = 'http://www.tusijie.com/user/check_user.php?email='+emailaddress;
    res = $.ajax({
        async: false,
        type: "GET",
        url: url,
    }).responseText;
	var resObj = eval('(' + res + ')');
	if (resObj.code==1)
		return resObj.has_user;

    return 0;
}
//判断昵称重复
function checkIfNickExist(nickname) {
    var url = 'http://www.tusijie.com/user/check_user.php?nickname='+nickname;
    res = $.ajax({
        async: false,
        type: "GET",
        url: url,
    }).responseText;
//	alert(res);
	var resObj = eval('(' + res + ')');
	if (resObj.code==1)
		return resObj.has_user;

    return 0;
}
function keydown() {
    $('#emailaddress_hint').hide();
    var $input = $('#emailaddress');
    if ($input.val() == "请输入注册时使用的邮箱") {
        $input.val('');
        $input.css('color', 'black')
    }
    if ($input.val() != "请输入注册时使用的邮箱" && $input.val() != "") {
        $input.css('color', 'black');
    }
    return true;
}
function dingti() {
    var $ti = $('#yincang_gzq');
    $ti.hide();
}
function showHintInfo(section, info) {
    $(section).html(info);
    $(section).show();
}